package com.dsg.service.account.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.dao.account.DsgAccountCheckSectionMapper;
import com.dsg.entity.account.DsgAccountCheckDetail;
import com.dsg.entity.account.DsgAccountCheckSection;
import com.dsg.service.account.IDsgAccountCheckDetailService;
import com.dsg.service.account.IDsgAccountCheckSectionService;
import com.dsg.util.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 开票号段 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-07-15
 */
@Service
public class DsgAccountCheckSectionServiceImpl extends ServiceImpl<DsgAccountCheckSectionMapper, DsgAccountCheckSection> implements IDsgAccountCheckSectionService {

    @Autowired
    private DsgAccountCheckSectionMapper dsgAccountCheckSectionMapper;
    @Autowired
    private IDsgAccountCheckDetailService dsgAccountCheckDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(DsgAccountCheckSection dsgAccountCheckSection) {
        BigDecimal beginCheckNo = dsgAccountCheckSection.getBeginCheckNo();
        BigDecimal endCheckNo = dsgAccountCheckSection.getEndCheckNo();
        if (beginCheckNo == null || endCheckNo == null) {
            throw new RRException("支票開始號和結束號不能空！");
        }
        //校驗號碼合法性
        if (beginCheckNo.compareTo(endCheckNo) > 0) {
            throw new RRException("支票開始號不能大於結束號！");
        }
        //判斷已存在的最大支票號
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("max(END_CHECK_NO) as endCheckNo");
        queryWrapper.eq("ENT_CODE", SingletonLoginUtils.getSystemEntCode());
        queryWrapper.eq("BANK_NAME", dsgAccountCheckSection.getBankName());
        DsgAccountCheckSection dacs = dsgAccountCheckSectionMapper.selectOne(queryWrapper);
        if (dacs != null) {
            if ((dacs.getEndCheckNo().add(BigDecimal.ONE)).compareTo(dsgAccountCheckSection.getBeginCheckNo()) != 0) {
                throw new RRException(String.format("支票開始號[%s]不能與已存在的支票號[%s]斷號!", dsgAccountCheckSection.getBeginCheckNo(), dacs.getEndCheckNo()));
            }
        }
        //保存支票號碼段
        dsgAccountCheckSection.setOid(UUIDGenerator.getUUID());
        save(dsgAccountCheckSection);
        //保存开票記錄
        List<DsgAccountCheckDetail> dsgAccountCheckDetailList = new ArrayList<>();
        while (beginCheckNo.intValue() <= endCheckNo.intValue()) {
            DsgAccountCheckDetail dsgAccountCheckDetail = new DsgAccountCheckDetail();
            dsgAccountCheckDetail.setOid(UUIDGenerator.getUUID());
            dsgAccountCheckDetail.setPid(dsgAccountCheckSection.getOid());
            dsgAccountCheckDetail.setCheckNo(beginCheckNo);
            dsgAccountCheckDetail.setStatus(DsgAccountCheckDetail.AppStatus.notInvoiced.getCode());
            dsgAccountCheckDetailList.add(dsgAccountCheckDetail);
            beginCheckNo = beginCheckNo.add(BigDecimal.ONE);
        }
        dsgAccountCheckDetailService.saveBatch(dsgAccountCheckDetailList);
    }
}
